package cn.foolishbird.hamal.frame.config;

import cn.foolishbird.hamal.frame.constant.IsEnum;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private final static String CREATE_TIME = "createTime";

    private final static String UPDATE_TIME = "updateTime";

    private final static String DEL_FLAG = "delFlag";

    @Override
    public void insertFill(MetaObject metaObject) {
        long create = System.currentTimeMillis();
        this.strictInsertFill(metaObject, CREATE_TIME, Long.class, create);
        this.strictInsertFill(metaObject, UPDATE_TIME, Long.class, create);
        this.strictInsertFill(metaObject, DEL_FLAG, Integer.class, IsEnum.FALSE.getState());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, UPDATE_TIME, Long.class, System.currentTimeMillis());
    }
}
